package test.my.com.myapplicationtest.desc_image;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import test.my.com.myapplicationtest.R;

/**
 * Created by ThinkPad on 2016/1/27.
 */
public class DescImage extends View {


    private int mWidth;
    private int mHeight;

    private Paint mPaint;
    private Rect mRect;
    private Rect mTextBound;

    private Bitmap mDescImage;

    private String mDescTextContent;
    private int mDescTextSize;
    private int mDescTextColor;
    private int mDescTextBackgroundColor;
    private int mDescTextBackgroundHeight;

    public DescImage(Context context) {
        super(context);
    }

    public DescImage(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DescImage(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.DescImage, defStyleAttr, 0);
        int n = ta.getIndexCount();
        for(int i=0;i<n;i++){
            int attr = ta.getIndex(i);
            switch (attr){
                case R.styleable.DescImage_descImage:
                    this.mDescImage = BitmapFactory.decodeResource(getResources(),ta.getResourceId(attr,0));
                    break;
                case R.styleable.DescImage_descTextContent:
                    this.mDescTextContent = ta.getString(attr);
                    break;
                case R.styleable.DescImage_descTextSize:
                    this.mDescTextSize = ta.getDimensionPixelOffset(attr,(int) TypedValue
                            .applyDimension(TypedValue.COMPLEX_UNIT_SP, 10,
                                    getResources().getDisplayMetrics()));
                    break;
                case R.styleable.DescImage_descTextColor:
                    this.mDescTextColor = ta.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.DescImage_descTextBackgrondColor:
                    this.mDescTextBackgroundColor = ta.getColor(attr,Color.WHITE);
                    break;
                case R.styleable.DescImage_descTextBackgroundHeight:
                    this.mDescTextBackgroundHeight = ta.getDimensionPixelSize(attr, (int) TypedValue
                            .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16,
                                    getResources().getDisplayMetrics()));
                    break;
            }
        }
        ta.recycle();
        mPaint = new Paint();
        mRect = new Rect();
        mTextBound = new Rect();
        mPaint.setTextSize(mDescTextSize);
        mPaint.getTextBounds(mDescTextContent, 0, mDescTextContent.length(), mTextBound);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);

        if(specMode == MeasureSpec.EXACTLY){ // match_parent accurate
            mWidth = specSize;
        } else {
            if(specMode == MeasureSpec.AT_MOST){
                mWidth = Math.min(mDescImage.getWidth(),specSize);
            }
        }

        specMode = MeasureSpec.getMode(heightMeasureSpec);
        specSize = MeasureSpec.getSize(heightMeasureSpec);

        if(specMode == MeasureSpec.EXACTLY){ // match_parent accurate
            mHeight = specSize;
        } else {
            if(specMode == MeasureSpec.AT_MOST){
                mHeight = Math.min(mDescImage.getHeight(),specSize);
            }
        }
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawBitmap(mDescImage, 0, 0, mPaint);

        // todo 阴影的高度应该根据字体的大小来改变，不应自己来设置
        mRect.set(0, mHeight - mDescTextBackgroundHeight, mWidth, mHeight);
        mPaint.setColor(mDescTextBackgroundColor);
        canvas.drawRect(mRect, mPaint);

        float textTop = mHeight - (mDescTextBackgroundHeight/2-mTextBound.height()/2);
        mPaint.setColor(mDescTextColor);
        canvas.drawText(mDescTextContent,0,textTop,mPaint);

    }
}
